(function($) {
/*whatever jQuery stuff*/
	$(function(){
		/*Check Box*/
		$('input[class*="parent-"]').click(function(){
			var ele = $(this).attr('class').match(/parent-(.+?)\b/);		
			if($(this).is(':checked')){
				$('input[class="child-'+ele[1]+'"]').attr('checked',true);
			} else {
				$('input[class="child-'+ele[1]+'"]').attr('checked',false);
			}
		});
		$('input[class*="child-"]').click(function(){
			var ele = $(this).attr('class').match(/child-(.+?)\b/);		
			if($(this).is(':checked')){
				$('input[class="parent-'+ele[1]+'"]').attr('checked',true);
			} else if(!$('input[class="child-'+ele[1]+'"]').is(':checked')) {
				$('input[class="parent-'+ele[1]+'"]').attr('checked',false);	
			}
		});
		
		/*Multiple Select*/		
		$('.addSelected').each(function(){
			$(this).click(function(){
				var Master = $(this).parent().parent(); 
  				return !$(Master).find('.box1 option:selected').remove().appendTo(Master.find('.box2'));  				
			});
		});
 		$('.removeSelected').each(function(){
			$(this).click(function(){
				var Master = $(this).parent().parent(); 
  				return !$(Master).find('.box2 option:selected').remove().appendTo(Master.find('.box1'));  			
			});
		});
			
		/*Delete Image*/
		$('.deleteImage').each(function(){
			$(this).click(function(){
				var Master = $(this).parent();
				if(confirm('Do you want to delete this?')){
						$.ajax({
							url:$(this).attr('href'),
							type:'post',
							async:false,
							success: function(data){
								Master.remove();
							}
						});
				}
				return false;
			});
		});
												
	});	
})(jQuery);

function delete_Form(e,ID,href){
	if($('#'+ID+'').find('tbody input[type="checkbox"]:checked').val()){
		if(confirm('Do you want to delete this?')){
			$('#'+ID+'').attr('action',href);
			$('#'+ID+'').submit();	
		}
	} else{
		alert('Please select item to delete.');
	}
	return false;
}

function update_Form(e,ID,href){
	if(confirm('Do you want to update this?')){
		$('#'+ID+'').attr('action',href);
		$('#'+ID+'').submit();
	}
	return false;
}

function addclone(e,tag,addtag,nottag){
	var clone = $(e).parent().find(tag).eq(0).clone();
	//clear value
	clone.find('select,input,textarea').val('');
	if(addtag!=undefined){
		var current_eq = $(e).parent().find(tag).length;
		clone.find(addtag+' select,'+addtag+' input,'+addtag+' textarea').each(function(){
			$(this).attr('name',$(this).attr('name').replace( /(\d+)/, current_eq));
		});
	}
	if(nottag!=undefined){
		clone.find(nottag).remove();
	}
	$(e).parent().find(tag).eq(-1).after(clone);
}

function removeclone(e,tag){
	if($(e).parent().find(tag).length == 1){
		$(e).parent().find(tag).find('select,input,textarea').val('');
	} else {
		$(e).parent().find(tag).not(':eq(0)').eq(-1).remove();
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

/*Filter*/
function toggleFilter(e)
{
	$('#filter_panel').slideToggle();
}

/*Copy Clipboard*/
function toClipboard(e)
{
	ZeroClipboard.setMoviePath(web_path+"ZeroClipboard.swf");
	var clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	copything = $(e).attr('title');
	clip.setText(copything.replace('you2playdev','you2play')); //replace you2playdev to you2play
	if (clip.div){
		clip.receiveEvent('mouseout', null);
		clip.reposition(e);
	} else {
		clip.glue(e);
	}
	clip.receiveEvent('mouseover', null);

	//When User Copy Link
	clip.addEventListener('complete', function (client, text) {
		alert('คัดลอกข้องมูลลง Clipboard เรียบร้อย');
	});	
}

/*Add/Remove List*/
function addRemoveList(unique,id,name)
{
	if(id == undefined && name == undefined){ //remove List
		var parentElement = $(unique).closest('ul');
		$(unique).parent().remove();
		if($('li:not(:hidden)',parentElement).length < 1){
			parentElement.find('.first').show();
		}	
	} else { //add List
		var parentElement = $('#'+unique).closest('ul');
		parentElement.find('.first').hide();
		if($('input[value="'+id+'"]',parentElement).length == 0){
			parentElement.prepend('<li><input type="checkbox" name="default_'+unique+'[]" value="'+id+'" checked="checked"/>&nbsp;'+id+' - '+name+' <a href="javascript:void(0)" class="tools minus" onclick="javascript:addRemoveList(this);"></a><input type="hidden" name="'+unique+'[]" value="'+id+'" \/><\/li>');
		}		
	}
}